const add = (model,params,ctx) => {
  return model.create(params).then((res)=> {
    if(res){
      ctx.body = {
        code:200,
        msg:'注册成功',
        data:res
      }
    }else{
      ctx.body = {
        code:300,
        msg:'注册失败',
      }
    }
  }).catch((err)=>{
    ctx.body = {
      code:400,
      msg:'注册出现异常'+err.message,
    }
  })
}


const update = (model,params,ctx) => {
  return model.updateOne(params).then((res)=>{
    ctx.body = {
      code:200,
      msg:'修改成功',
      data:res
    }
  }).catch((err)=>{
    ctx.body = {
      code:400,
      msg:'修改失败'+err.message,
    }
  })
}

const del = (model,params,ctx) => {
  return model.findOneAndDelete(params).then((res)=>{
    ctx.body = {
      code:200,
      msg:'删除成功',
      data:res
    }
  }).catch((err)=>{
    ctx.body = {
      code:400,
      msg:'删除失败'+err.message,
    }
  })
}

const find = (model,params,ctx) => {
  return model.find().then((res)=>{
    ctx.body = {
      code:200,
      msg:'查询成功',
      data:res
    }
  }).catch((err)=>{
    ctx.body = {
      code:400,
      msg:'查询失败'+err.message,
    }
  })
}

const findOne = (model,params,ctx) => {
  return model.findOne(params).then((res)=>{
    ctx.body = {
      code:200,
      msg:'查询成功',
      data:res
    }
  }).catch((err)=>{
    ctx.body = {
      code:400,
      msg:'查询失败'+err.message,
    }
  })
}

module.exports = {
  add,
  update,
  del,
  find,
  findOne
}